package com.service.impl;

import com.DTO.PageParam;
import com.DTO.UpdateTwitterDTO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.entity.Twitter;
import com.entity.TwitterComment;
import com.entity.TwitterPicture;
import com.entity.User;
import com.mapper.TwitterCommentMapper;
import com.mapper.TwitterMapper;
import com.mapper.TwitterPictureMapper;
import com.service.TwitterPictureService;
import com.service.TwitterService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Set;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author XTC
 * @since 2024-07-10
 */
@Service
public class TwitterServiceImpl extends ServiceImpl<TwitterMapper, Twitter> implements TwitterService {
    @Autowired
    private TwitterMapper twitterMapper;


    @Autowired
    private TwitterPictureMapper twitterPictureMapper;

    @Autowired
    private TwitterCommentMapper twitterCommentMapper;

    @Override
    public Page<Twitter> getByUserId(PageParam pageParam, Integer userId) {
        QueryWrapper<Twitter> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId)
                .orderByDesc("publish_time");
        Page<Twitter> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.twitterMapper.selectPage(page, queryWrapper);
    }

    @Override
    public Page<Twitter> getTwitterOfFriends(PageParam pageParam, List<Integer> friendIdList) {
        QueryWrapper<Twitter> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("user_id", friendIdList)
                .orderByDesc("publish_time");
        Page<Twitter> page = new Page<>(pageParam.getCurrent(), pageParam.getSize());
        return this.twitterMapper.selectPage(page, queryWrapper);
    }

    @Override
    public boolean update(UpdateTwitterDTO updateTwitterDTO) {
        List<String> urls = updateTwitterDTO.getUrls();
        QueryWrapper<TwitterPicture> wrapper = new QueryWrapper<>();
        wrapper.eq("twitter_id", updateTwitterDTO.getId());
        List<TwitterPicture> twitterPictureList = this.twitterPictureMapper.selectList(wrapper);
        for( TwitterPicture twitterPicture : twitterPictureList){
            if(!urls.contains(twitterPicture.getFileUrl())) {
                this.twitterPictureMapper.deleteById(twitterPicture.getId());
            }
        }
        UpdateWrapper<Twitter> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", updateTwitterDTO.getId())
                .set("content", updateTwitterDTO.getContent());
        return this.twitterMapper.update(null, updateWrapper) > 0;
    }

    @Override
    public boolean delete(Integer twitterId) {
        this.twitterCommentMapper.delete(new QueryWrapper<TwitterComment>().eq("twitter_id", twitterId));
        this.twitterPictureMapper.delete(new QueryWrapper<TwitterPicture>().eq("twitter_id", twitterId));
        return this.twitterMapper.deleteById(twitterId) > 0;
    }
}
